package com.fzhucc.modules.subject.service;

import com.fzhucc.base.PageDTO;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType2CreateDto;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType2QueryCriteria;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType2QueryDto;
import com.fzhucc.modules.subject.domain.entity.SubjectHighfieldsType2;
import com.fzhucc.modules.subject.repository.SubjectHighfieldsType2Repository;
import com.fzhucc.modules.subject.service.mapstruct.SubjectHighfieldsType2Mapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @website https://fzhucc.com
* @description 服务实现
* @author yyg
* @since 2022-09-26 00:00:00
**/
@Service
@RequiredArgsConstructor
public class SubjectHighfieldsType2Service {

    private final SubjectHighfieldsType2Repository subjectHighfieldsType2Repository;
    private final SubjectHighfieldsType2Mapper subjectHighfieldsType2Mapper;

    
    public PageDTO<SubjectHighfieldsType2QueryDto> queryAll(SubjectHighfieldsType2QueryCriteria criteria, Pageable pageable){
        Page<SubjectHighfieldsType2> page = subjectHighfieldsType2Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageDTO.by(page.map(subjectHighfieldsType2 -> subjectHighfieldsType2Mapper.toQueryDto(subjectHighfieldsType2)));
    }

    
    public List<SubjectHighfieldsType2QueryDto> queryAll(SubjectHighfieldsType2QueryCriteria criteria){
        List<SubjectHighfieldsType2> subjectHighfieldsType2List = subjectHighfieldsType2Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        return subjectHighfieldsType2List.stream().map(subjectHighfieldsType2 -> subjectHighfieldsType2Mapper.toQueryDto(subjectHighfieldsType2)).collect(Collectors.toList());
    }

    
    @Transactional
    public SubjectHighfieldsType2QueryDto findById(Integer id) {
        SubjectHighfieldsType2 subjectHighfieldsType2 = subjectHighfieldsType2Repository.findById(id).orElseGet(SubjectHighfieldsType2::new);
        ValidationUtil.isNull(subjectHighfieldsType2.getId(),"SubjectHighfieldsType2","id",id);
        return subjectHighfieldsType2Mapper.toQueryDto(subjectHighfieldsType2);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public SubjectHighfieldsType2QueryDto create(SubjectHighfieldsType2CreateDto resources) {
        return subjectHighfieldsType2Mapper.toQueryDto(subjectHighfieldsType2Repository.save(subjectHighfieldsType2Mapper.toEntity(resources)));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(SubjectHighfieldsType2CreateDto resources) {
        SubjectHighfieldsType2 subjectHighfieldsType2 = subjectHighfieldsType2Repository.findById(resources.getId()).orElseGet(SubjectHighfieldsType2::new);
        ValidationUtil.isNull( subjectHighfieldsType2.getId(),"SubjectHighfieldsType2","id",resources.getId());
        subjectHighfieldsType2.copy(subjectHighfieldsType2Mapper.toEntity(resources));
        subjectHighfieldsType2Repository.save(subjectHighfieldsType2);
    }

    
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            subjectHighfieldsType2Repository.deleteById(id);
        }
    }

    
    public void download(List<SubjectHighfieldsType2QueryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SubjectHighfieldsType2QueryDto subjectHighfieldsType2 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("highfields2",  subjectHighfieldsType2.getHighfields2());
            map.put("name",  subjectHighfieldsType2.getName());
            map.put("sort",  subjectHighfieldsType2.getSort());
            map.put("highfields1",  subjectHighfieldsType2.getHighfields1());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}